﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace FormsAppT8
{
    public partial class DataLink : Form
    {
        public DataLink()
        {
            InitializeComponent();
        }

        private void DataLink_Load(object sender, EventArgs e)
        {
            //this.AcceptButton = this.button1;
            //this.FormBorderStyle = FormBorderStyle.FixedDialog;

            XmlDocument xDoc = new XmlDocument();
            //XmlTextWriter myWriterxsl;
            XmlElement mybook;
            XmlElement mybookA;

            UserSQLClass usql = new UserSQLClass();
            string fileName = Application.StartupPath + "\\login.xml";
            try
            {
                //读文件内容，加载历史数据；没有时初始配置文件
                if (!File.Exists(fileName))
                {
                    usql.initLogin();                
                }
                else
                {
                    xDoc.Load(fileName);
                    XmlElement xElem1 = (XmlElement)xDoc.SelectSingleNode("//sqlStr1");
                    if (xElem1 != null)
                    {
                        textBox1.Text = xElem1.GetAttribute("serverName");
                        textBox2.Text = xElem1.GetAttribute("dataName");
                        textBox3.Text = xElem1.GetAttribute("userName");
                       // textBox4.Text = usql.Getpass(xElem1.GetAttribute("pass"));
                    }
                    else
                    {
                        mybook = xDoc.CreateElement("sqlStr1");
                        mybook.SetAttribute("serverName", "192.168.0.10");
                        mybook.SetAttribute("dataName", "sqlname");
                        mybook.SetAttribute("userName", "sa");
                        mybook.SetAttribute("pass", "IZ7Zjm5VHg8=");  //"123456"
                        mybook.SetAttribute("comNo", "COM3");
                        mybook.SetAttribute("time", "2018-08-08 08:08:08");
                        mybookA = xDoc.CreateElement("sqlStr1");
                        mybookA.SetAttribute("serverName", "192.168.0.10");
                        mybookA.SetAttribute("dataName", "sqlname");
                        mybookA.SetAttribute("userName", "sa");
                        mybookA.SetAttribute("pass", "IZ7Zjm5VHg8=");  //"123456"
                        mybookA.SetAttribute("time", "2018-08-08 08:08:08");
                        xDoc.DocumentElement.AppendChild(mybook);
                        xDoc.DocumentElement.AppendChild(mybookA);
                        xDoc.Save(fileName);
                    }
                }

            }
            catch
            {
            }
            button3.Focus();

        }


        private void button1_Click(object sender, EventArgs e)
        {
            UserSQLClass usql = new UserSQLClass();
            SqlConnection Mycon = new SqlConnection();
            try
            {
                string server = textBox1.Text;
                string dataName = textBox2.Text;
                string user = textBox3.Text;
                string pass = textBox4.Text;
                string sqlStr = string.Format("Data Source = {0};Initial Catalog = {1};User Id = {2};Password = {3}",
                               server, dataName, user, pass);

                //数据库连接相关
                Mycon.ConnectionString = sqlStr;
                //("Data Source=.;Initial Catalog=Member;User ID=sa");
                Mycon.Open();
                if (Mycon.State == ConnectionState.Open)
                {
                    MessageBox.Show("连接测试服务器成功", "信息提示",
                                     MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Mycon.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "连接测试服务器失败,请检查", "错误提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Mycon.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument xDoc = new XmlDocument();
            XmlTextWriter myWriterxsl;
            XmlElement mybook;

            UserSQLClass usql = new UserSQLClass();
            string fileName = Application.StartupPath + "\\login.xml";

            try
            {
                //读文件内容，加载历史数据；没有时初始配置文件
                if (!File.Exists(fileName))
                {
                    myWriterxsl = new XmlTextWriter(fileName, Encoding.UTF8);
                    myWriterxsl.Formatting = Formatting.Indented;
                    myWriterxsl.WriteStartDocument();

                    myWriterxsl.WriteStartElement("login");
                    myWriterxsl.WriteEndElement();
                    myWriterxsl.WriteEndDocument();
                    myWriterxsl.Flush();
                    myWriterxsl.Close();
                }       
                    xDoc.Load(fileName);

                    XmlElement xElem1;
                if (radioButton1.Checked)
                {
                    xElem1 = (XmlElement)xDoc.SelectSingleNode("//sqlStr1");
                }
                else
                {
                    xElem1 = (XmlElement)xDoc.SelectSingleNode("//sqlStr");
                }                    
                
                if (xElem1 == null)
                {
                    if (radioButton1.Checked)
                    {
                        mybook = xDoc.CreateElement("sqlStr1");
                    }
                    else
                    {
                        mybook = xDoc.CreateElement("sqlStr");
                    }

                    mybook.SetAttribute("serverName", textBox1.Text.Trim());
                    mybook.SetAttribute("dataName", textBox2.Text.Trim());
                    mybook.SetAttribute("userName", textBox3.Text.Trim());
                    mybook.SetAttribute("pass", usql.Setpass(textBox4.Text.Trim()));
                    mybook.SetAttribute("comNo", "COM3");
                    mybook.SetAttribute("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    xDoc.DocumentElement.AppendChild(mybook);
                }
                else
                {
                    xElem1.SetAttribute("serverName", textBox1.Text.Trim());
                    xElem1.SetAttribute("dataName", textBox2.Text.Trim());
                    xElem1.SetAttribute("userName", textBox3.Text.Trim());
                    xElem1.SetAttribute("pass", usql.Setpass(textBox4.Text.Trim()));
                    xElem1.SetAttribute("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                    xDoc.Save(fileName);
                //Common.SqlString = usql.ConfigGetValue();
                MessageBox.Show("连接信息保存成功", "信息提示",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "保存失败,请检查", "错误提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            //if (System.Diagnostics.Process.GetProcessesByName("TabTip").Length <1)
            //{
            //    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
            //}
            //if (System.Diagnostics.Process.GetProcessesByName("TabTip").Length < 1)
            //{
            //    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
            //    //System.Diagnostics.Process.Start(@"C:\Windows\System32\osk.exe");
            //}
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            //if (System.Diagnostics.Process.GetProcessesByName("TabTip").Length < 1)
            //{
            //    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
            //}
            // System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            //if (System.Diagnostics.Process.GetProcessesByName("TabTip").Length < 1)
            //{
            //    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
            //}
            // System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");             
        }
         private void textBox4_Click(object sender, EventArgs e)
        {
            //if (System.Diagnostics.Process.GetProcessesByName("TabTip").Length < 1)
            //{
            //    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");
            //}
            // System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe");           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
